package com.jinbaoty.recyclebins.command;

import android.os.Handler;
import android.os.Message;

import com.alibaba.fastjson.JSONObject;
import com.hoho.android.usbserial.driver.UsbSerialPort;
import com.jinbaoty.recyclebins.command.CommandTask;
import com.jinbaoty.recyclebins.server.APIService;
import com.jinbaoty.recyclebins.server.OrderForm;
import com.jinbaoty.recyclebins.server.ResponseData;


import java.util.concurrent.TimeUnit;

public class ScanTask implements Runnable{
    public static final int ORDER_CREATE=0x7e;
    private UsbSerialPort port;
    private Handler serialStatusHanlder;
    private boolean taskRunning = true;
    private OrderForm orderForm=new OrderForm();

    public OrderForm getOrderForm() {
        return orderForm;
    }

    public void setOrderForm(OrderForm orderForm) {
        this.orderForm = orderForm;
    }

    public boolean isTaskRunning() {
        return taskRunning;
    }

    public void setTaskRunning(boolean taskRunning) {
        this.taskRunning = taskRunning;
    }
    private boolean cancle = false;

    public boolean isCancle() {
        return cancle;
    }

    public void setCancle(boolean cancle) {
        this.cancle = cancle;
    }

    public ScanTask(UsbSerialPort port, Handler serialStatusHanlder){
        this.port = port;
        this.serialStatusHanlder = serialStatusHanlder;
    }

    @Override
    public void run() {
        //Logger.d("开始扫描订单");
        while(true){
            if(cancle) break;


//            ResponseData orderCreate = APIService.isOrderCreate();
            //Logger.d("订单："+orderCreate);

            if(orderForm!=null){
                //Logger.d("检测到订单："+orderCreate);
                Message msg = new Message();
                msg.what = ORDER_CREATE;
                msg.obj = orderForm;
                serialStatusHanlder.sendMessage(msg);
               // JSONObject orderForm = (JSONObject)orderCreate.getData();
                try {
                    Long wasteId = orderForm.getWasteId();
                    if (wasteId!=null) {
                        CommandTask commandTask = new CommandTask(port, serialStatusHanlder);
                        //SerialInputOutputManager serialInputOutputManager = new SerialInputOutputManager(port, commandTask);
                        //Executors.newSingleThreadExecutor().submit(serialInputOutputManager);

                        commandTask.startRecycleTask(orderForm);
                        orderForm=null;
                        setTaskRunning(false);
                        break;
                    }else{
                        TimeUnit.MILLISECONDS.sleep(200);
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }else{
                try {
                    TimeUnit.SECONDS.sleep(1l);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
